import type { NProgressOptions } from 'nprogress'
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

export const useNProgress = () => {
  const appStore = useAppStoreWithOut()
  const { systemConfig } = storeToRefs(appStore)

  NProgress.configure({
    // 动画方式
    easing: 'ease',
    // 递增进度条的速度
    speed: 500,
    // 是否显示加载ico
    showSpinner: true,
    // 自动递增间隔
    trickleSpeed: 200,
    // 初始化时的最小百分比
    minimum: 0.3
  } as NProgressOptions)

  const start = () => {
    if (systemConfig.value && systemConfig.value.isProgressEnable) {
      NProgress.start()
    }
  }

  const done = () => {
    if (systemConfig.value && systemConfig.value.isProgressEnable) {
      NProgress.done()
    }
  }

  return {
    start,
    done
  }
}
